package com.js.data.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.js.data.system.entity.SysMenu;
import com.js.data.system.entity.SysRoleMenu;
import com.js.data.system.mapper.SysMenuMapper;
import com.js.data.system.mapper.SysRoleMenuMapper;
import com.js.data.system.service.RoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色菜单关联 服务实现类
 * </p>
 *
 * @author wujj
 * @since 2022-09-06
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements RoleMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;

    /**
     * 保存角色菜单到数据库
     *
     * @param roleId  角色id
     * @param menuIds 菜单id
     */
    @Override
    public void saveBatchToDB(Long roleId, List<Long> menuIds) {
        if (CollectionUtil.isNotEmpty(menuIds)) {
            List<SysRoleMenu> roleMenuList = new ArrayList<>();
            menuIds.forEach(menuId -> {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenu.setMenuId(menuId);
                roleMenuList.add(sysRoleMenu);
            });
            saveBatch(roleMenuList);
        }
    }

    /**
     * 根据角色删除
     *
     * @param roleId 角色id
     */
    @Override
    public void removeByRoleId(Long roleId) {
        lambdaUpdate().eq(SysRoleMenu::getRoleId, roleId).remove();
    }

    /**
     * 根据菜单删除
     *
     * @param menuId 菜单id
     */
    @Override
    public void removeByMenuId(Long menuId) {
        lambdaUpdate().eq(SysRoleMenu::getMenuId, menuId).remove();
    }

    /**
     * 根据菜单批量删除
     *
     * @param menuIds 菜单ids
     */
    @Override
    public void removeByMenuIds(List<Long> menuIds) {
        lambdaUpdate().in(SysRoleMenu::getMenuId, menuIds).remove();
    }

    /**
     * 根据角色id获取
     *
     * @param roleId 角色id
     * @return List<Integer>
     */
    @Override
    public List<Long> listMenuIdByRoleId(Long roleId) {
        return lambdaQuery().eq(SysRoleMenu::getRoleId, roleId).list().stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
    }

    /**
     * 根据角色id获取
     *
     * @param roleIds 角色id
     * @return List<SysMenu>
     */
    @Override
    public List<SysMenu> listMenuIdByRoleIds(Collection<Long> roleIds, Integer state) {
        Set<Long> menuIds = lambdaQuery()
                .in(SysRoleMenu::getRoleId, roleIds).list()
                .stream().map(SysRoleMenu::getMenuId).collect(Collectors.toSet());
        if (CollectionUtil.isEmpty(menuIds)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(state)) {
            queryWrapper.eq(SysMenu::getState, state);
        }
        queryWrapper.in(SysMenu::getId, menuIds);
        return sysMenuMapper.selectList(queryWrapper);
    }
}
